﻿using DX_sharq.core;
using DX_sharq.Models;
using MyCrypto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DX_sharq.Controllers
{
    public class AccountController : Controller
    {
        Linq_sharqDB1DataContext db = new Linq_sharqDB1DataContext();
        CookieClass _Cookie = new CookieClass();

        Crypto _crypto = new Crypto();
        public ActionResult LogOn(string mess)
        {
            string ss = Request.QueryString["mess"];
            ViewData["mess"] = ss;
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(string login, string pass)
        {
            // DB ichidan login bor-yoqligini tekshirib chiqadi  (bor bolsa return 1)
            int t = db.get_access(login) ?? -1;  // биринчи ДБга мурожаат киладиган жойи

            string mess = "",
                ism = "",
                fam = "",
                kirish = "",
                admin = "",
                ishchi_id = "",
                ishchi_name = "",
                bulim_type = "",
                bulim_id = "",
                bulim_name = "";
            Nullable<int> user_id_orig = 0; ////////////////////////////////////////////////////////////////////////////////

            int ad = 0,
                kir = 0;
            int _proc;
            string mes = "";

            switch (t)
            {
                case -1:
                    mes = "Бунақа логин йўқ!";
                    break;
                case 2:
                    mes = "Ушбу логиндаги ходим ишчи ўрнига тайинланмаган!";
                    break;
                case 0:
                    mes = "Логин администратор томонидан вақтинча ёпилган ёки ҳали фаоллаштирилмаган!";
                    break;
                case 1:
                    string pass2 = _crypto.Decrypt(db.get_password(login), _crypto.sharedSec);
                    if (pass == pass2) // kiritilgan parol bn shu loginning DBdagi parolini tekshiradi
                    {

                        // DBdan shu parametrlae value-sini olib chiqadi
                        // "_app_ki", "_org_n", "_org_i", "_org_o", "_org_r" va xakozo
                        _proc = db.access
                            (
                                login,
                                pass,
                                ref mess,
                                ref kirish,
                                ref admin,
                                ref ism,
                                ref fam,
                                ref bulim_type,
                                ref bulim_id,
                                ref bulim_name,
                                ref ishchi_id,
                                ref ishchi_name,
                                ref user_id_orig
                            );
                        ad = Convert.ToInt32(admin);
                        kir = Convert.ToInt32(kirish);

                        // Shu parametrlarga cookie yozib chiqadi
                        _Cookie.WriteCookie
                            (
                                ad,
                                kir,
                                ism,
                                fam,
                                ishchi_id,
                                ishchi_name,
                                bulim_type,
                                bulim_id,
                                bulim_name,
                                login,
                                user_id_orig
                            );
                    }
                    else
                    {
                        mes = "Parol xato!";
                    }
                    break;
            }
            string ss = Request.QueryString["returnUrl"];
            if (!string.IsNullOrEmpty(ss))
            {
                return Redirect(ss);
            }
            else
            {
                return RedirectToAction("Index", "Home", new { mess = mes });
            }
        }

        public ActionResult LogOff()
        {
            _Cookie.ClearCookieAll();
            return RedirectToAction("LogOn");
        }

        public ActionResult HelpMain()
        {
            return View();
        }

    }
}
